<template>
  <div class="center">
    <!-- 头部 -->
    <div class="table_header flex flex_bet flex-wrap">
      <div class="table_button">
        <el-button type="primary" icon="el-icon-refresh" size="mini" @click="Refresh"></el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addDialog = true">提交需求</el-button>
      </div>
      <div class="table_searchs flex felx_align">
        <div class="table_search flex">
          <el-input placeholder="搜索" v-model="searchTable" class="input-with-select" size="small">
            <el-select v-model="select" slot="prepend" placeholder="请选择" class="table_select" style="width:105px">
              <el-option label="用户名" value="username"></el-option>
              <el-option label="需求类型" value="demand_type"></el-option>
            </el-select>
            <el-button slot="append" icon="el-icon-search" @click="idnameSearch"></el-button>
          </el-input>
        </div>
        <dynamicTable :checkboxVal="checkboxVal" @alter="alterCheckbok" />
      </div>
    </div>
    <!-- 专题管理table -->
    <el-table stripe :data="brandTable" style="width: 100%" :key="key" border fit highlight-current-row ref="tableDataRef">
      <el-table-column v-if="colData[0].istrue" prop="username" label="用户" min-width="110" align="center"></el-table-column>
      <el-table-column v-if="colData[1].istrue" prop="create_time" label="需求记录明细" min-width="140" align="center">
        <template slot-scope="scope">{{scope.row.create_time| newDate}}</template>
      </el-table-column>
      <el-table-column v-if="colData[2].istrue" prop="dName" label="需求型号" min-width="110" align="center"></el-table-column>
      <el-table-column v-if="colData[3].istrue" prop="demand_type" label="需求类型" min-width="100" align="center"></el-table-column>
      <el-table-column v-if="colData[4].istrue" prop="expect_time" label="期望完成日期" min-width="140" align="center">
        <template slot-scope="scope">{{scope.row.expect_time| numberDays}}</template>
      </el-table-column>
      <el-table-column v-if="colData[5].istrue" prop="plan_time" label="计划完成日期" min-width="140" align="center">
        <template slot-scope="scope">{{scope.row.plan_time| numberDays}}</template>
      </el-table-column>
      <el-table-column v-if="colData[6].istrue" label="状态" min-width="120" align="center">
        <template slot-scope="scope">
          {{scope.row.status == 1 ? '定制' : scope.row.status == 2 ? '审核' : '已审核' }}
          {{scope.row.status == 3 ? scope.row.type == 1 ? '- 未交付' : '- 已交付' : ''}}
        </template>
      </el-table-column>
      <el-table-column v-if="colData[7].istrue" label="硬件版本(SN前5位)" min-width="140" align="center">
        <template slot-scope="scope">
          <div v-for="(item,index) in scope.row.deliver" :key="item.id">{{ item.name_code ? `固件${index + 1}: ${item.name_code}` : ''}}</div>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[8].istrue" prop="plan_time" label="实际完成日期" min-width="140" align="center">
        <template slot-scope="scope">
          <div v-for="(item1,index1) in scope.row.deliver" :key="item1.id">{{ `固件${index1 + 1}:`}} {{item1.actual_time| numberDays}}</div>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[9].istrue" label="固件版本号" min-width="350" align="center">
        <template slot-scope="scope">
          <div v-for="(item2,index2) in scope.row.deliver" :key="item2.id">{{ item2.firmware_version ? `固件${index2 + 1}: ${item2.firmware_version}` : ''}}</div>
        </template>
      </el-table-column>
      <el-table-column v-if="colData[10].istrue" label="固件简单描述" min-width="120" align="center">
        <template slot-scope="scope">
          {{scope.row.deliver[0].remark}}
        </template>
      </el-table-column>
      <el-table-column v-if="colData[11].istrue" label="固件下载信息" min-width="120" align="center">
        <template slot-scope="scope">
          <div class="flex flex_cen felx_align" v-for="(item3,index3) in scope.row.deliver" :key="item3.id">
            {{`固件${index3 + 1}: `}}<i class="iconfont iconanzhuo download" style="border:none;background-color:inherit;color:#a4c872">
              <a target="_blank" :href="item3.firmware_address"></a>
            </i>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="300" align="center">
        <template slot-scope="scope">
          <el-row>
            <el-button size="mini" @click="checkDate(scope.row.id)">查看</el-button>
            <el-button v-permission="['超级管理员','固件管理员']" size="mini" type="primary" @click="auditDate(scope.row.id)">审核</el-button>
            <el-button v-permission="['超级管理员','固件管理员']" size="mini" @click="deliveryDate(scope.row.id)">{{scope.row.type == 1 ? '未交付' : '已交付'}}</el-button>
            <el-button v-permission="['超级管理员']" size="mini" @click="getdeleteBrowser(scope.row.id)">删除</el-button>
          </el-row>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="paging">
      <el-pagination :current-page.sync="listQuery.startSize" :page-sizes="[10, 50, 100, 200]" :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background
        @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
    </div>
    <!-- 添加表单start -->
    <el-dialog title="固件添加" :visible.sync="addDialog" @close="addFormclose" :close-on-click-modal="false" :close-on-press-escape="false" width="35%">
      <addFirm @closeAdd="closeAdd" @getqueryFirmwareCustom="getqueryFirmwareCustom" ref="addFirm" v-if="addDialog" />
      <div slot="footer" class="dialog-footer" style="text-align:right">
        <el-button @click="addDialog = false" size="mini" round>取 消</el-button>
        <el-button type="primary" @click="addFirmwares" size="mini" round>确 定</el-button>
      </div>
    </el-dialog>
    <!-- 添加表单end -->

    <!-- 审核表单start -->
    <el-dialog title="固件审核" :visible.sync="auditDialog" width="60%" :close-on-click-modal="false" :close-on-press-escape="false">
      <audit @closeAudit="closeAudit" @getqueryFirmwareCustom="getqueryFirmwareCustom" ref="audit" :auditId="auditId" v-if="auditDialog" />
      <div slot="footer" class="dialog-footer" style="text-align:right">
        <el-button @click="auditDialog = false" size="mini" round>取 消</el-button>
        <el-button type="primary" @click="auditFirm" size="mini" round>确 定</el-button>
      </div>
    </el-dialog>
    <!-- 审核表单end -->

    <!-- 交付表单start -->
    <el-dialog title="固件交付" :visible.sync="deliveryDialog" :close-on-click-modal="false" :close-on-press-escape="false">
      <delivery @closeDelivery="closeDelivery" @getqueryFirmwareCustom="getqueryFirmwareCustom" ref="delivery" :deliveryId="deliveryId" v-if="deliveryDialog" />
      <div slot="footer" class="dialog-footer" style="text-align:right">
        <el-button @click="deliveryDialog = false" size="mini" round>取 消</el-button>
        <el-button type="primary" @click="deliveryFirm" size="mini" round>确 定</el-button>
      </div>
    </el-dialog>
    <!-- 交付表单end -->

    <!-- 查看表单start -->
    <el-dialog title="查看信息" :visible.sync="checkDialog" width="60%" :close-on-click-modal="false" :close-on-press-escape="false">
      <check ref="check" :checkId="checkId" v-if="checkDialog" />
      <div slot="footer" class="dialog-footer" style="text-align:right">
        <el-button @click="checkDialog = false" size="mini" round>取 消</el-button>
        <el-button type="primary" size="mini" round>确 定</el-button>
      </div>
    </el-dialog>
    <!-- 查看表单end -->

    <delete-Dialog title="" :visible.sync="deleteDialogs.show" width="25%" top="40vh" @deleteEvents="deleteEvents" :deleteText="deleteDialogs.deleteText" />
  </div>
</template>

<script>
import dynamicTable from "@/components/DynamicTable";
import objectable from "@/utils/objectable.js";
import deleteDialog from "@/components/deleteDialog";
import permission from '@/directive/permission/index.js' // 权限判断指令
import { queryFirmwareCustom, deleteCustom } from "@/api/firmware.js";
import addFirm from '../components/addFirmware.vue'
import audit from '../components/auditFirm.vue'
import delivery from '../components/delivery.vue'
import check from '../components/check.vue'
export default {
  directives: { permission },
  components: { dynamicTable, deleteDialog, addFirm, audit, delivery, check },
  data() {
    return {
      checkboxVal: ['用户', '需求记录明细', '需求型号', '需求类型', '期望完成日期', '计划完成日期', '状态', '硬件版本(SN前5位)', '实际完成日期', '固件版本号', '固件简单描述', '固件下载信息'],
      key: 1,
      colData: [],//动态控制table
      brandTable: [],
      select: "", //搜索条件
      searchTable: "",
      total: 0, //总数量
      listQuery: { startSize: 1, pageSize: 10, username: undefined, demand_type: undefined, userId: localStorage.getItem("ID") },
      addDialog: false,
      auditDialog: false,
      deliveryDialog: false,
      checkDialog: false,
      //审核ID
      auditId: 0,
      //交付ID
      deliveryId: 0,
      checkId: 0,
      deleteDialogs: { show: false, deleteText: '', deleteId: 0, sn: '' },
    }
  },
  created() {
    this.colData = objectable(this.checkboxVal, this.checkboxVal);//转为数组对象，好控制table
    this.getqueryFirmwareCustom()
  },
  methods: {
    getdeleteBrowser(id) {
      this.deleteDialogs.show = true
      this.deleteDialogs.deleteId = id
    },
    //删除按钮事件
    deleteEvents() {
      let params = { userId: localStorage.getItem("ID"), id: this.deleteDialogs.deleteId }
      deleteCustom(params).then(res => {
        this.$message.success(res.msg)
        this.getqueryFirmwareCustom()
      })
    },
    //查看事件-----start
    checkDate(id) {
      this.checkId = id
      this.checkDialog = true
    },
    //查看事件-----end

    //交付事件-----start
    deliveryDate(id) {
      this.deliveryId = id
      this.deliveryDialog = true
    },
    deliveryFirm() {
      this.$refs.delivery.getdeliverFirmwareDeliver()
    },
    closeDelivery() {
      this.deliveryDialog = false
    },
    //交付事件-----end

    //审核事件-----start
    auditDate(id) {
      this.auditDialog = true
      this.auditId = id
    },
    auditFirm() {
      this.$refs.audit.getexamineFirmwareCustom()
    },
    closeAudit() {
      this.auditDialog = false
    },
    //审核事件-----end

    //添加事件------start
    //增加定制需求
    addFirmwares() {
      this.$refs.addFirm.getaddFirmwareCustom()
    },
    closeAdd() {
      this.addDialog = false
    },
    //添加弹窗关闭事件
    addFormclose() {
      this.$refs.addFirm.addFormclose()
    },
    //添加事件-----end
    getqueryFirmwareCustom() {
      queryFirmwareCustom(this.listQuery).then(res => {
        this.brandTable = res.data
        this.total = res.total
      }).catch(err => {
        this.brandTable = []
        this.total = 0
      })
    },
    //分页
    handleSizeChange(val) {
      this.listQuery.pageSize = val
      this.getqueryFirmwareCustom();
    },
    handleCurrentChange(val) {
      this.listQuery.startSize = val
      this.getqueryFirmwareCustom();
    },
    //搜索,1，专题名称
    idnameSearch() {
      if (!this.select) return this.$message.warning("请先选择查询条件");
      this.listQuery = Object.assign({}, this.$options.data().listQuery, { [this.select]: this.searchTable || undefined })
      this.getqueryFirmwareCustom()
    },
    Refresh() {
      this.listQuery = this.$options.data().listQuery
      this.getqueryFirmwareCustom()
    },
    //下拉动态table子组件
    alterCheckbok(key, conte) {
      this.key = key;
      this.colData.filter(i => {
        if (conte.indexOf(i.title) != -1) {
          i.istrue = false;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        } else {
          i.istrue = true;
          this.$nextTick(() => {
            this.$refs.tableDataRef.doLayout();
          });
        }
      });
    }
  }
}
</script>

<style scoped>
/deep/ .el-form-item__label {
  font-weight: 500;
}
/deep/ .selectPopper {
  display: none;
}
</style>